# author Ruoyan
# tell mean of var_id_2 group by var_id_1

group_mean <- function(data = data.frame(), group_by = "var_id_1", values = "var_id_2"){
  order <- paste("id_temp <- unique(data$", group_by, ")", sep = "")
  eval(parse(text = order))
  
  means_temp <- rep(NA, length(id_temp))
  
  for(i in 1:length(id_temp)){
    order <- paste("means_temp[i] <- mean(data[data$", group_by, 
                   " == id_temp[i],]$", values, ", na.rm = T)", sep = "")
    eval(parse(text = order))
  }
  
  temp_df <- data.frame(id_temp, means_temp)
  names(temp_df) <- c(group_by, values)
  return(temp_df)
}

# temp_v1 <- rep(c("type_1", "type_2"), 200)
# temp_v2 <- seq(2,104,length = 400)
# 
# temp_df <- data.frame(temp_v1, temp_v2)
# head(temp_df)
# group_mean(data = temp_df, group_by = "temp_v1", values = "temp_v2")
